package com.xxwy.ddu.cms.authonrize.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xxwy.ddu.cms.authonrize.entity.BaseUser;
import com.xxwy.ddu.cms.authonrize.service.IUserRolesService;
import com.xxwy.ddu.cms.authonrize.service.IUserService;
import com.xxwy.ddu.common.exception.IllegalParameterException;
import com.xxwy.ddu.common.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xxwy
 * @since 2018-08-13
 */
@RestController
@Slf4j
public class BaseUserController {

    @Autowired
    IUserService iUserService;

    @Autowired
    IUserRolesService iUserRolesService;

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Autowired
    private PasswordEncoder encoder;


    /**
     * 注册
     *  应该写在service层的
     * @param baseUser
     * @return
     */
    @PostMapping("/user/registry")
    public ResponseEntity<ModelMap> signUpUser(@RequestBody @Valid BaseUser baseUser, HttpServletRequest request) {
        EntityWrapper<BaseUser> wrapper = new EntityWrapper<>();
        wrapper.eq("u_name", baseUser.getuName());
        BaseUser look_username = baseUser.selectOne(wrapper);
        //用户名不存在
        if (look_username == null) {
            baseUser.setuPassword(encoder.encode(baseUser.getuPassword()));
            baseUser.setBlock(false);
            baseUser.setDeleteFlag(0);
            baseUser.setuCreatetime(new Date());
            baseUser.insert();
            socialSignUp(baseUser, request, wrapper);
            return RUtil.success();
        } else {
            throw new IllegalParameterException("用户名已存在");
        }
    }

    private void socialSignUp(@Valid @RequestBody BaseUser baseUser, HttpServletRequest request, EntityWrapper<BaseUser> wrapper) {
        //判断是不是第三方
        Connection<?> connectionFromSession = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
        if (connectionFromSession != null) {
            BaseUser look_id = baseUser.selectOne(wrapper);
            providerSignInUtils.doPostSignUp(look_id.getuId().toString(), new ServletWebRequest(request));
        }
    }

    /**
     * 绑定,只针对浏览器
     *
     * @param baseUser
     * @param request
     * @return
     */
    @PostMapping("/user/bingding")
    public ResponseEntity bingding(@RequestBody @Valid BaseUser baseUser, HttpServletRequest request) {
        BaseUser baseUser1 = baseUser.selectById();
        if (baseUser1 != null) {
            if (baseUser1.getuPassword() == encoder.encode(baseUser.getuPassword())) {
                //绑定
                providerSignInUtils.doPostSignUp(baseUser1.getuId().toString(), new ServletWebRequest(request));
                return RUtil.success();
            } else {
                throw new IllegalParameterException("用户或者密码错误");
            }
        } else {
            throw new IllegalParameterException("用户不存在");
        }

    }



}
